package com.ruoyi.dictionaries.controller;

import java.util.List;


import com.ruoyi.common.utils.uuid.IdUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dictionaries.domain.Nation;
import com.ruoyi.dictionaries.service.INationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 民族管理Controller
 *
 * @author gdslovehy
 * @date 2025-04-01
 */
@RestController
@RequestMapping("/dictionaries/nation")
public class NationController extends BaseController
{
    @Autowired
    private INationService nationService;

    /**
     * 查询民族管理列表
     */
    //@PreAuthorize("@ss.hasPermi('dictionaries:nation:list')")
    @GetMapping("/list")
    public TableDataInfo list(Nation nation)
    {
        startPage();
        List<Nation> list = nationService.selectNationList(nation);
        return getDataTable(list);
    }

    /**
     * 导出民族管理列表
     */
    @PreAuthorize("@ss.hasPermi('dictionaries:nation:export')")
    @Log(title = "民族管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Nation nation)
    {
        List<Nation> list = nationService.selectNationList(nation);
        ExcelUtil<Nation> util = new ExcelUtil<Nation>(Nation.class);
        util.exportExcel(response, list, "民族管理数据");
    }

    /**
     * 获取民族管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('dictionaries:nation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(nationService.selectNationById(id));
    }

    /**
     * 新增民族管理
     */
    @PreAuthorize("@ss.hasPermi('dictionaries:nation:add')")
    @Log(title = "民族管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Nation nation)
    {
        nation.setId(IdUtils.fastSimpleUUID());
        return toAjax(nationService.insertNation(nation));
    }
    @PreAuthorize("@ss.hasPermi('dictionaries:nation:add')")
    @Log(title = "民族管理", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult excelImport(MultipartFile file) throws Exception {
        ExcelUtil<Nation> util = new ExcelUtil<Nation>(Nation.class);
        List<Nation> nationsList = util.importExcel(file.getInputStream());
        for (Nation nation : nationsList) {
            nation.setId(IdUtils.fastSimpleUUID()); // 给每条数据添加UUID
        }
        return toAjax(nationService.insertNationList(nationsList));
    }
    /**
     * 修改民族管理
     */
    @PreAuthorize("@ss.hasPermi('dictionaries:nation:edit')")
    @Log(title = "民族管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Nation nation)
    {
        return toAjax(nationService.updateNation(nation));
    }

    /**
     * 删除民族管理
     */
    @PreAuthorize("@ss.hasPermi('dictionaries:nation:remove')")
    @Log(title = "民族管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(nationService.deleteNationByIds(ids));
    }
}
